package hello;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;
import auction.chapter08.ItemFKToNonPK;
import auction.chapter08.UserFKToCompPK;
import auction.chapter08.UserFKToNonPK;

public class HelloWorldFKToNonPK
{
	
	
	public static void main(String[] args)throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();
		
		
		UserFKToNonPK u=new UserFKToNonPK();
		u.setFirstName("wei");
		u.setLastName("sun");
		u.setCustomerNr("9526");
		
		ItemFKToNonPK i=new ItemFKToNonPK();
		i.setTitle("ttt");
		i.setSeller(u);
		
		//s.save(u);
		//s.save(i);
		
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		/*
		ItemFKToNonPK loadi=(ItemFKToNonPK)s1.load(ItemFKToNonPK.class, 1L);
		out(loadi.getSeller().getCustomerNr());
		*/
		UserFKToNonPK loadu=(UserFKToNonPK)s1.load(UserFKToNonPK.class, 1L);
		out(loadu.getItemsForAuction().size());
		
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println("---->"+obj);
	}
}
